<?php
namespace app\home\controller;

use app\common\model\HelpToUpstart;
use app\common\component\help_to_upstart\HelpToUpstartService;

class ScalpingUpstart extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();

		if(getParam('UPSTART_IS_OPEN') == 0) {
			echo '<meta charset="utf-8"><script>alert("土豪区已关闭!");window.history.go(-1);</script>';exit;
		}
	}

	public function index() {
		(new HelpToUpstartService()) -> overtime();

		$helpToUpstarts = HelpToUpstart::alias('h_t_u')
			-> join('league_help_get_upstart h_g_u', 'h_g_u.help_get_upstart_id = h_t_u.help_get_upstart_id')
			-> join('league_user u', 'h_g_u.user_id = u.user_id')
			-> field(
				'h_t_u.help_to_upstart_id, h_t_u.help_get_upstart_id, h_t_u.user_id h_t_u_user_id, h_t_u.order_id, h_t_u.price, h_t_u.create_time, h_t_u.begin_time, h_t_u.over_time, h_t_u.status, ' .
				'h_g_u.user_id h_g_u_user_id, ' .
				'u.username, u.realname, u.phone, u.bank_code'
			)
			-> where('h_t_u.user_id', \think\Session::get('user_id'))
			-> order('h_t_u.status ASC, h_g_u.help_to_upstart_id DESC')
			-> paginate(10);

		$this -> view -> assign('helpToUpstarts', $helpToUpstarts);

        return $this -> view -> fetch();
	}

	public function confirmHelpToUpstart() {
		$post = $this -> request -> post();
		if($this -> validate($post, ['help_to_upstart_id' => 'require|token']) !== true) {
			return json(['code' => -1, 'message' => '请勿重复提交!']);
		}

		return json((new HelpToUpstartService()) -> confirm($post['help_to_upstart_id']));
	}
}
?>